<template>
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field
                    v-model="userName"
                    name="用户名"
                    label="用户名"
                    placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]"
            />
            <van-field
                    v-model="userAccount"
                    name="账号"
                    label="账号"
                    placeholder="账号"
                    :rules="[{ required: true, message: '请填写账号' }]"
            />
            <van-field
                    v-model="userPassword"
                    type="password"
                    name="密码"
                    label="密码"
                    placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]"
            />
            <van-field
                    v-model="checkPassword"
                    type="password"
                    name="确认密码"
                    label="确认密码"
                    placeholder="确认密码"
                    :rules="[{ required: true, message: '请确认密码' }]"
            />
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button round block type="primary" native-type="submit">
                提交
            </van-button>
        </div>
    </van-form>
</template>

<script setup>
    import {ref} from 'vue'
    import myAxois from "../utils/MyAxios";
    import {showFailToast, showSuccessToast} from "vant";
    import {useRouter} from 'vue-router';

    const userName = ref('');
    const userAccount = ref('');
    const userPassword = ref('');
    const checkPassword = ref('');

    const router = useRouter();

    const onSubmit = async () =>{
        const res = await myAxois.post("user/register",{
            name:userName.value,
            userAccount:userAccount.value,
            userPassword:userPassword.value,
            checkPassword:checkPassword.value,
        })
        if(res.data.code === 0){
            router.push({
                path:"/login",
                replace:true
            })
            showSuccessToast("注册成功");
        }else{
            showFailToast("注册失败")
        }
    }

</script>

<style scoped>

</style>